package com.zzq.my.jetpacks.jectpackapp.ui.act

import androidx.lifecycle.lifecycleScope
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.rxjava.rxlife.LifecycleScope
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.databinding.ActivityFlowBinding
import com.zzq.my.jetpacks.util.viewLifeCycleOwner
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.system.measureTimeMillis

/**
 * 描述：flow 的基本使用
 * 作者：zhuangzeqin
 * 时间: 2022/1/18-17:04
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class FlowActivity : BaseActivity(R.layout.activity_flow) {
    private val vb by viewBinding(ActivityFlowBinding::bind)

    /**
     * 初始化数据
     */
    override fun initData() {

    }

    /**
     * 初始化操作
     */
    override fun initView() {

        lifecycleScope.launchWhenCreated {

        }
        lifecycleScope.launch {

        }
        val async = lifecycleScope.async {
            ""
        }

    }

    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {
        vb.stvFlow.setOnClickListener {
            runBlocking {
                //1 创建Flow
//                flowOf("a","b","c").onStart {
//                   println("开始 ........");
//                }.onCompletion {
//                    println("结束........")
//                }.map {
//                    it+"平安顺"
//                }.collect {
//                    println(it)
//                }

                //2 创建Flow 的第二个方式 集合转化
                listOf(1,2,3,4,5).asFlow().onStart {
                    println("开始")
                }.onCompletion {
                    println("结束")
                }.map {
                    it.toString()+"平安顺"
                }.flowOn(Dispatchers.IO).collect {
                    println(it)
                }

               val time =  measureTimeMillis {
                    channelFlow {
                        for (i in 1..5) send(i)
                    }.collect {
                        println(it)
                    }
                }
                println("time$time")

            }


        }

    }
}